package com.opg.uop.manage.system.service;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.opg.uop.manage.base.UOPRestApiException;
import com.opg.uop.manage.base.UOPServiceResponse;
import com.opg.uop.manage.system.model.User;

//@FeignClient("uop-system-server")
//@Service
public class UserService {
	
//	@RequestMapping(method=RequestMethod.GET, value="/users")
//	UOPRestApiResponse getUserList(@RequestParam("pageSize") int pageSize ,@RequestParam("page") int page);
	
	@Autowired
	private RestTemplate restTemplate;
	
	public User securityToLogin(String username,String password){
		Map<String, String> map = new HashMap<String,String>();
		User user = null;
		map.put("username", username);
		map.put("password", password);
		try {
			UOPServiceResponse uopServiceResponse = restTemplate
					.postForEntity("http://uop-system-server/users/", null, UOPServiceResponse.class, map).getBody();
			String code = uopServiceResponse.getMeta().get("code").toString();
			if (UOPServiceResponse.FAILED.equals(code)) {
				throw new UOPRestApiException(uopServiceResponse.getError().get("message").toString());
			} else {
				user = (User) uopServiceResponse.getData();
			}
		} catch (Exception e) {
			throw new UOPRestApiException(e);
		}
		return user;
	}
}
